<template>
<view class="search-container">
  <!--  收索框-->
  <view class="search-box">
    <uni-search-bar  @input="input" :radius="100" cancelButton="none"></uni-search-bar>
  </view>
  <!-- //收索提示 -->
  <view class="search-list" v-if="searchResults.length!==0">
    <view class="search-item" v-for="(item,i) in searchResults" :key="i" @click="gotoGoodsDetails(item.goods_id)">
      <view class="goods-name">
        {{item.goods_name}}
      </view>
      <uni-icons type="arrowright" size="16"></uni-icons>
    </view>
  </view>
  <!-- //历史记录 -->
  <view class="history-box" v-else>
    <view class="history-title">
      <view class="history-text">
        收索历史
      </view>
      <uni-icons type='trash' size="17" @click="cleanHistoryList"></uni-icons>
    </view>
    <view class="history-conten">
      <uni-tag :text="item" v-for="(item, i) in histroys" :key="i" @click="gotoGoodsDetails(item)"></uni-tag>
    </view>
  </view>
</view>
  
  
</template>

<script>
  export default {
    data() {
      return {
        timer:null,
        inputIfo:'',//存放输入内容
        searchResults:[],
        historyList:['a','b','c']
      };
    },
    methods:{
      //输入处理
      input(e){
        //防抖处理
        clearInterval(this.timer)//关闭定时器
        this.timer=setTimeout(()=>{//开启定时器
           console.log(e)
           this.inputIfo=e
           this.getResults()
        },500)
      },
      //获取推荐列表
      async getResults(){
        if(this.inputIfo===''){
          this.searchResults=[]
          return
        }
        const {data:res}=await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.inputIfo})
        // console.log(res)
        if(res.meta.status!==200){
          uni.showMsg()
        }
        this.searchResults=res.message
        this.saveSearch()
        console.log(this.searchResults)
      },
      //将收索内容添加到历史记录中
      saveSearch(){
        // this.historyList.push(this.inputIfo)
        const set=new Set(this.historyList)
        set.delete(this.inputIfo)
        set.add(this.inputIfo)
        
        this.historyList=Array.from(set)
        // console.log(this.historyList)
         //保存收索历史记录到本地
         uni.setStorageSync('inputIfo',JSON.stringify(this.historyList))
      },
     
      //跳转到商品页
      gotoGoodsDetails(id){
        uni.navigateTo({
          url:"/subpak/goods-details/goods-details?goods_id"+id
        })
        // console.log(id)
      },
      //删除历史记录
      cleanHistoryList(){
        // console.log("444")
        this.historyList=[],
        uni.setStorageSync("inputIfo",[])
      },
      // 点击历史记录跳转到商品页
      gotoGoodsDetails(item){
        uni.navigateTo({
          url:"/subpak/goods_list/goods_list?qure="+item
        })
      }
    },
    computed:{
      histroys(){
        return [...this.historyList].reverse()
      }
    },
    onLoad(){
      this.historyList=JSON.parse(uni.getStorageSync('inputIfo')||'[]')
    }
  }
</script>

<style lang="scss">
.search-box{
  position: sticky;
  top:0 ;
  z-index: 999;
}
.search-list{
  padding: 0 30rpx;
  .search-item{
    padding: 30rpx 0;
    border-bottom: 1px solid #000;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .goods-name{
      white-space: nowrap;//不允许换行
      overflow: hidden;//溢出隐藏
      text-overflow: ellipsis;//超出部分以...代替
      margin-right: 3px;
    }
  }
}
.history-box{
  padding: 0 10rpx;
  .history-title{
    display: flex;
    justify-content:space-between;
    align-items: center;
    height: 80rpx;
    font-size: 28rpx;
    border-bottom: 2rpx solid #E9EBEC;
  }
  .history-conten{
    margin-top: 10rpx;
    margin-right: 10rpx;
    .uni-tag{
      margin-top: 10rpx;
      margin-right: 10rpx;
      background-color: #ccc;
      border: #ccc;
    }
  }
}

</style>
